﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Collections;
using ECCIT;
using ECCIT.Web;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Data;
using System.IO;

public partial class admin_UKNOW_event_event_add : AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_EVENT_ID = "eid";
    private const string VIEWSTATE_IMG_LIST = "img_list";
    private const string VIEWSTATE_IMG_TOP = "img_top";
    private const string VIEWSTATE_IMG_VENUE = "img_venue";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];

            string sEventsID = RequestContext.GetParamValueByParamName(VIEWSTATE_EVENT_ID, "");
            ViewState[VIEWSTATE_EVENT_ID] = sEventsID;
            ViewState[VIEWSTATE_IMG_LIST] = "";
            ViewState[VIEWSTATE_IMG_TOP] = "";
            ViewState[VIEWSTATE_IMG_VENUE] = "";

            if (string.IsNullOrEmpty(sEventsID))
                InitPage(null, 0);
            else
                InitPage(int.Parse(sEventsID), 0);
        }
    }

    private void InitPage(int? eventsID, int type)
    {
        InitControls();

        EventsModel model = null;
        if (eventsID.HasValue)
        {
            EventsBLL bll = new EventsBLL(this.LangID);
            model = bll.selEvent(eventsID.Value);
        }

        BindEventsToControls(model, type);
    }

    private void BindEventsToControls(EventsModel info, int type)
    {

        if (ViewState[VIEWSTATE_EVENT_ID]=="")
        {
            infoname.Text = "新增活动";

            if (info == null)
            { return; }
        }
        else
        {
            infoname.Text = "修改活动";
        }

        title.Text = info.Title;
        txtSubTitle.Text = info.SubTitle;
        area_ddl.SelectedValue = (info.City.HasValue) ? info.City.Value.ToString() : "";
        address_txt.Text = info.Venue;
        art_ddl.SelectedValue = (info.ArtFormID.HasValue) ? info.ArtFormID.ToString() : "";

        string sStartTime = (info.StartTime.HasValue) ? info.StartTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
        if (sStartTime.EndsWith(" 00:00:00")) sStartTime = sStartTime.Substring(0, 10);
        txtStartTime.Text = sStartTime;

        string sEndTime = (info.EndTime.HasValue) ? info.EndTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
        if (sEndTime.EndsWith(" 00:00:00")) sEndTime = sEndTime.Substring(0, 10);
        txtEndTime.Text = sEndTime;
        sponsors_ddl.SelectedValue = (info.SponsorID.HasValue) ? info.SponsorID.ToString() : "";

        summary_txt.Text = info.ShortDescription;
        content.Text = info.Detail;
        hot_rbt.SelectedValue = info.IsHot ? "1" : "0";
        recom_rbt.SelectedValue = info.IsRecommendation ? "1" : "0";
        indextop.SelectedValue = info.IsHomepageTop ? "1" : "0";
        columntop.SelectedValue = info.IsColumnTop ? "1" : "0";
        pub.SelectedValue = info.State.ToString();
        templateId.SelectedValue = (info.TemplateID.HasValue) ? info.TemplateID.Value.ToString() : "";

        txtTicketUrl.Text = info.TicketUrl;
        chkIsSeriesMain.Checked = (type==0) ? info.IsSeriesMain : false;
        ddlSeries.SelectedValue = (info.SeriesMainEventsID.HasValue) ? info.SeriesMainEventsID.Value.ToString() : "";

        //将图片路径保存在ViewState里面，用于在没有修改图片的情况下保存数据的时候从ViewState里面将原来的图片路径取出来保存到数据库
        ViewState[VIEWSTATE_IMG_LIST] = info.ListImg;
        ViewState[VIEWSTATE_IMG_TOP] = info.TopImg;
        ViewState[VIEWSTATE_IMG_VENUE] = info.VenueImagePath;
    }

    private void InitControls()
    {
        
        //获取字典里的活动城市
        this.area_ddl.Items.Clear();
        DataSet ds = new DictionaryManageBLL().GetDictionary(" upid = '208' order by cnname");
        for (int i = 0; i < ds.Tables.Count; i++)
        {
            DataTable table = ds.Tables[i];
            foreach (DataRow row in table.Rows)
            {
                this.area_ddl.Items.Add(new ListItem(row["cnname"].ToString(), row["CID"].ToString()));
            }
        }

        //获取 活动的艺术形式
        this.art_ddl.Items.Clear();
        IList<ArtFormModel> Artlist = new ArtFormBLL().selectArt(LangID);
        foreach (ArtFormModel art in Artlist)
        {
            this.art_ddl.Items.Add(new ListItem(art.Name, art.ID.ToString()));
        }

        //获取赞助商
        this.sponsors_ddl.Items.Clear();
        this.sponsors_ddl.Items.Add(new ListItem("无", ""));

        IList<SponsorModel> Sponsorlist = new SponsorBLL(LangID).selectSponsor();//mff
        foreach (SponsorModel spon in Sponsorlist)
        {
            this.sponsors_ddl.Items.Add(new ListItem(spon.Name, spon.ID.ToString()));
        }

        //初始模板
        this.templateId.Items.Clear();
        ModelManageBLL mmb = new ModelManageBLL();
        DataSet temdata = mmb.GetModel("");
        for (int i = 0; i < temdata.Tables.Count; i++)
        {
            DataTable table = temdata.Tables[i];
            for (int j = 0; j < table.Rows.Count; j++)
            {
                DataRow row = table.Rows[j];

                this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));

            }
        }

        // 获取系列活动数据
        this.ddlSeries.Items.Clear();
        IList<EventsModel> events = new EventsBLL(this.LangID).GetSeriesMainEvents();
        ddlSeries.Items.Add(new ListItem("--请选择--", ""));
        foreach (EventsModel evt in events)
            ddlSeries.Items.Add(new ListItem(evt.Title, evt.ID.ToString()));
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        EventsBLL bll = new EventsBLL(this.LangID);
        EventsModel info = new EventsModel();

        string filename = "";
        string topImgName = "";
        string venueMapFileName = null;
        string path = Server.MapPath("~/uploadfiles/image/");
        //列表图上传
        if(file.PostedFile.ContentLength>0)
        {
            string name = Guid.NewGuid().ToString();
            string ext = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - (file.FileName.LastIndexOf(".")));
            filename = name + ext;
            if (File.Exists(path + filename))
            {
                File.Delete(path + filename);
            }
            else
            {
                file.SaveAs(path + filename);
            }
        }
        //置顶图上传
        if (filetop.PostedFile.ContentLength > 0)
        {
            string ext = filetop.FileName.Substring(filetop.FileName.LastIndexOf("."), filetop.FileName.Length - (filetop.FileName.LastIndexOf(".")));
            string topname = Guid.NewGuid().ToString();
            topImgName = topname + ext;
            if (File.Exists(path + topImgName))
            {
                File.Delete(path + topImgName);
            }
            else
            {
                filetop.SaveAs(path + topImgName);
            }
        }

        //活动举办地点地图上传
        if (fileVenueMap.PostedFile.ContentLength > 0)
        {
            string name = Guid.NewGuid().ToString();
            string ext = fileVenueMap.FileName.Substring(fileVenueMap.FileName.LastIndexOf("."), fileVenueMap.FileName.Length - (fileVenueMap.FileName.LastIndexOf(".")));
            venueMapFileName = name + ext;
            if (File.Exists(path + venueMapFileName))
                File.Delete(path + venueMapFileName);
            else
                fileVenueMap.SaveAs(path + venueMapFileName);
        }

        info.ID = null;
        if (!string.IsNullOrEmpty(ViewState[VIEWSTATE_EVENT_ID].ToString()))
           info.ID = int.Parse(ViewState[VIEWSTATE_EVENT_ID].ToString());

        info.Title = title.Text;
        info.SubTitle = txtSubTitle.Text;
        info.LanguageID = LangID;
        info.City = int.Parse(area_ddl.SelectedValue);
        info.Venue = address_txt.Text;
        info.ArtFormID = int.Parse(art_ddl.SelectedValue);

        info.StartTime = null;
        info.EndTime = null;
        DateTime startTime, endTime;
        if (DateTime.TryParse(txtStartTime.Text, out startTime))
            info.StartTime = startTime;

        if (DateTime.TryParse(txtEndTime.Text, out endTime))
            info.EndTime = endTime;

        info.SponsorID = null;
        if (sponsors_ddl.SelectedValue != string.Empty)
            info.SponsorID = int.Parse(sponsors_ddl.SelectedValue);

        info.ShortDescription = summary_txt.Text;
        info.Detail = content.Text;
        info.IsHot = hot_rbt.SelectedItem.Selected;
        info.IsRecommendation = recom_rbt.SelectedValue == "0" ? false : true;
        info.IsHomepageTop = indextop.SelectedValue == "0" ? false : true;
        info.IsColumnTop = columntop.SelectedValue == "0" ? false : true;
        info.ColumnID = int.Parse(ViewState[VIEWSTATE_COLUMN_ID].ToString());
        info.LayerIDs = ViewState[VIEWSTATE_LAYER_ID].ToString();
        info.State = int.Parse(pub.SelectedValue);
        info.ListImg = string.IsNullOrEmpty(filename) ? ViewState[VIEWSTATE_IMG_LIST].ToString() : filename;
        info.TopImg = string.IsNullOrEmpty(topImgName) ? ViewState[VIEWSTATE_IMG_TOP].ToString() : topImgName;
        info.TemplateID = int.Parse(templateId.SelectedValue);

        info.TicketUrl = txtTicketUrl.Text;
        info.VenueImagePath = string.IsNullOrEmpty(venueMapFileName) ? ViewState[VIEWSTATE_IMG_TOP].ToString() : venueMapFileName;
        info.IsSeriesMain = chkIsSeriesMain.Checked;
        info.SeriesMainEventsID = null;
        if (!string.IsNullOrEmpty(ddlSeries.SelectedValue))
            info.SeriesMainEventsID = int.Parse(ddlSeries.SelectedValue);

        ResultInfo res=bll.Save(info);

        if (res.Result)
        {
            string sid = ddlSeries.SelectedValue;
            if (!string.IsNullOrEmpty(sid)&&synMedia.Checked)
            {
                EventsModel srcEventsModel = new EventsModel();
                srcEventsModel.LanguageID = info.LanguageID;
                srcEventsModel.ID = int.Parse(sid);

                bll.SyncEventsAttachments(srcEventsModel, info);
            }
            Response.Redirect("event_list.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID=" + LangID);
        }
        else
        {
            Response.Write("<script>alert('保存失败')</script>");
            Response.Write(res.Message);
        }

    }

    protected void chkShowEndTime_CheckedChanged(object sender, EventArgs e)
    {
        trEndTime.Visible = !trEndTime.Visible;
        if (!trEndTime.Visible)
            txtEndTime.Text = string.Empty;
    }

    protected void ddlSeries_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sEventsID = ddlSeries.SelectedValue;
        if (string.IsNullOrEmpty(sEventsID)) return;

        int eventsID = int.Parse(sEventsID);
        InitPage(eventsID, 1);
    }

    protected void chkIsSeriesMain_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void lbBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("event_list.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID=" + LangID);
    }
}
